package beans;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.apache.jasper.tagplugins.jstl.core.Out;

import beans.Student;

// import com.sun.corba.se.pept.transport.Connection;
// import com.sun.org.apache.regexp.internal.recompile;

public class StudentDao {
	String DB_URL = "jdbc:mysql://localhost:8889/mydb?useUnicode=true&characterEncoding=utf-8";
	String DB_USER = "root";
	String DB_PASSWORD = "root";
	public  ArrayList QueryAllStudents() throws Exception {
		Connection conn = null;
		ArrayList students = new ArrayList();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
			String sql = "SELECT STUNO, STUNAME FROM STUDENT";
			Statement stat = conn.createStatement();
			ResultSet rs = stat.executeQuery(sql);
			System.out.println(rs);
			while (rs.next()) {
				Student student = new Student();
				student.setStuno(rs.getString("STUNO"));
				student.setStuname(rs.getString("STUNAME"));
				students.add(student);
			}
			rs.close();
			stat.close();
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				if (conn != null) {
					conn.close();
					conn = null;
				}
			} catch (Exception ex) {
				// TODO: handle exception
			}
		}
		return students;
	}
}
